package pl.planito.manager;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import pl.planito.company.manager.CompanyBridge;
import pl.planito.company.manager.ICompanyEditorLocal;
import pl.planito.company.manager.ICompanyEditorRemote;
import pl.planito.entity.Company;

/**
 * Session Bean implementation class CompanyEditor
 */
@Stateless
public class CompanyEditorImpl implements ICompanyEditorLocal,
		ICompanyEditorRemote {
	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Default constructor.
	 */
	public CompanyEditorImpl() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	public List<CompanyBridge> getAllCompanies() {
		List<pl.planito.entity.Company> companies = entityManager
				.createNamedQuery("allCompanies").getResultList();
		List<CompanyBridge> companies2 = new ArrayList<CompanyBridge>();

		for (pl.planito.entity.Company company : companies) {
			companies2.add(toCompanyBridge(company));
		}

		return companies2;
	}

	public void addCompany(CompanyBridge company) {

		entityManager.persist(toCompany(company));

	}

	public CompanyBridge editCompany(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	private Company toCompany(CompanyBridge companyBridge) {
		Company company = new Company();
		company.setCity(companyBridge.getCity());
		company.setName(companyBridge.getName());
		company.setPhoneNumber(companyBridge.getPhoneNumber());
		company.setStreet(companyBridge.getStreet());
		company.setZipCode(companyBridge.getZipCode());
		return company;
	}

	private CompanyBridge toCompanyBridge(Company company) {

		CompanyBridge company2 = new CompanyBridge();
		company2.setCity(company.getCity());
		company2.setName(company.getName());
		company2.setPhoneNumber(company.getPhoneNumber());
		company2.setStreet(company.getStreet());
		company2.setZipCode(company.getZipCode());

		return company2;
	}

}
